###########################################################
#     Prj: WGCNA all in one
#     Assignment: filter hub
#     Author: Shawn Wang
#     Date: Mar 24,2021
###########################################################
suppressMessages(library(tidyverse))
suppressMessages(library(getopt))
command=matrix(c(
  'help', 'h', 0, 'logic', 'help information',
  'sGs', 'g', 1, 'double', 'GS cutoff',
  'skME', 'k', 1, 'double', 'kME cutoff',
  'rawhub', 'r', 1, 'character', 'raw hubgene table'
),byrow = T, ncol = 5)
args = getopt(command)

## default valuels==========
if (!is.null(args$help)) {
  cat(paste(getopt(command, usage = T), "\n"))
  #  q(status=1)
}
if (is.null(args$sGs)){
  args$sGs = 0.8
}
if (is.null(args$skME)){
  args$skME = 0.9
}
rawhub = args$rawhub
sGs = args$sGs
skME = args$skME
print(sGs)
print(skME)
if (is.null(args$rawhub)){
  q(status=1)
}
## test
# rawhub = "05.hubgene.xls"
# sGs = 0.8
# skME = 0.9
hub <- read.table(rawhub,header = T,sep = "\t")

shub <- hub %>% 
  group_by(module) %>% 
  mutate(abs_GS = abs(GS),abs_kME = abs(kME)) %>% 
  filter(abs_GS > sGs) %>% 
  filter(abs_kME > skME)

write.table(x = shub,file = "05.hubgene.filtered.xls",row.names = F,sep = "\t",quote = F)